package com.example.json;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.PreparedQuery;
import com.google.appengine.api.datastore.Query;

public class Stock 
{
	private static DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
	
	public static void createStock(String name, Double quantity, String formula) 
	{
		Entity Stock = getSingleStock(name);
		
		if ( Stock == null )
		{	
			Stock = new Entity("StockEntity", name);
			long nextCode = StockCode.getNextStockCode();
			Stock.setProperty("Code", "C" + nextCode);
			StockCode.putStockCode(nextCode);	
		}	
		
		Stock.setProperty("Quantity", quantity);
		Stock.setProperty("Formula", formula);	
		
		Util.persistEntity(Stock);			
	}
	
	public static void updateStock(String name, Double quantity ) 
	{
		Entity Stock = getSingleStock(name);
		
		if (Stock != null) 
		{
			Stock.setProperty("Quantity", quantity);
			String formula = (String)Stock.getProperty("Formula");	
						
			if (formula.isEmpty() == false )
			{
				for (String stocks: formula.split(","))
				{
			         String[] coefficients = stocks.split("C");
			         Double weightage = Double.parseDouble(coefficients[0]);
			         Entity dependentStock = getSingleStockbyCode("C" + coefficients[1]);
			         
			         Double dependentQuantity = (Double)dependentStock.getProperty("Quantity");
			         dependentQuantity = dependentQuantity - (weightage*quantity);
			         
			         updateStockQuantity(dependentStock, dependentQuantity);
			    }
			}
			
			Util.persistEntity(Stock);
		}
	}
	
	public static void updateStockQuantity (Entity Stock, Double quantity)
	{
		Stock.setProperty("Quantity", quantity);
		Util.persistEntity(Stock);
		
		String formula = (String)Stock.getProperty("Formula");
		
		if (formula.isEmpty() == false )
		{
			for (String stocks: formula.split(","))
			{
		         String[] coefficients = stocks.split("C");
		         Double weightage = Double.parseDouble(coefficients[0]);
		         Entity dependentStock = getSingleStockbyCode("C" + coefficients[1]);
		         
		         Double dependentQuantity = (Double)dependentStock.getProperty("Quantity");
		         dependentQuantity = dependentQuantity - (weightage*quantity);
		         updateStockQuantity(dependentStock, dependentQuantity);
		    }
		}		
	}

	public static Iterable<Entity> getAllStocks() 
	{
		Iterable<Entity> entities = Util.listEntities("StockEntity", null, null);
		return entities;
	}

	public static Iterable<Entity> getStock(String name) 
	{
		Iterable<Entity> entities = Util.listEntities("StockEntity", "name", name);
		return entities;
	}

	public static Entity getSingleStock(String name) 
	{
		Key key = KeyFactory.createKey("StockEntity", name);
		return Util.findEntity(key);
	}
	
	@SuppressWarnings("deprecation")
	public static Entity getSingleStockbyCode(String code) 
	{
		Query q = new Query("StockEntity").addFilter("Code", Query.FilterOperator.EQUAL,code);
		
	    PreparedQuery pq = datastore.prepare(q);
	    
	    for (Entity result : pq.asIterable()) 
	    {
	    	return result;
	    }
	    
	    // this added to avoid compile errors, needs to be fixed.
		Key key = KeyFactory.createKey("StockEntity", code);
		return Util.findEntity(key);
	}	
}
